## Computation of vol_q(n,r) and ub_sphere(n,d,q)

'''
volume(n,r,q) yields the volumen of the Hamming
ball of radius r in the Hamming space of length n
over a q-ary alphabet.

ub_sphere(n,d,q) delivers the Hamming sphere upper bound
for q-ary codes of lengthe n and minimum distance d
'''

from PyM import *

def volume(n,r,q=2):
    v = 0
    b = q-1
    for i in range(r+1):
        v += binom(n,i) * b**i
    return v
    
show(volume(9,3))

show(volume(9,3,3))

def ub_sphere(n,d,q=2):
    return floor(q**n/volume(n,floor((d-1)/2),q))
    
show(ub_sphere(8,3))
show(ub_sphere(9,3))
show(ub_sphere(10,3))
show(ub_sphere(11,3))
show(ub_sphere(11,3,3))
show(ub_sphere(21,5))

